#include<stdio.h>
#include<assert.h>
#ifndef __DEBUG__
#define debug_msg(fmt, args...)
#define debug_msg1(fmt)
#else
#define debug_msg(fmt, args...) printf(fmt, ##args) 
#define debug_msg1(fmt) assert(fmt)
#endif
int len(char *p)
{
    int total = 0;
    while(*p!='\0')
    {
        *p++;
        total++;
    }
    return total;
}
void even_card(char *p,int len)
{
    debug_msg1(len != 0);//如果长度为0 ，用D——DEBUG——进行测试，用gcc不会显示，不会执行这条指令
    debug_msg("%d\n",len);
    int odd = 0;
    int even = 0;
    int total =0;
    int i = len;
    int a;
    for(i = len ;i > 0;i--)
    {
        if(i%2 != 0)
        {
            a = ( *(p+i-1)-'0')*2>9?1:0;
            debug_msg("i  %d  a %d\n",i,a);
            if(a)
            {
                even = even +( (*(p+i-1)-'0')*2-9);
            }else
            {
                even = even + 2*(*(p+i-1)-'0');
            }                
        }else
        {
            odd = odd + (*(p+i-1)-'0');
        }
    }
    total = odd + even;
    debug_msg("odd is%d\n",odd);
    debug_msg("total is%d\n",total);
    debug_msg("even is%d\n",even);
    if(total%10==0)
    {
        printf("successful!!right card number!\n");
    }else
    {
        printf("this is not a card number!\n");
    }
}
void odd_card(char *p,int len)
{
    int odd = 0;
    int even = 0;
    int total = 0;
    int i = len;
    for(i = len ;i > 0;i--)
    {
        if(i%2 == 0)
        {
            if((*(p+i-1)-'0')*2 > 9)
            {
                even = even + (*(p+i-1)-'0')*2-9;
            }else
            {
                even = even + (*(p+i-1)-'0')*2;
            }                
        }else
        {
            odd = odd + *(p+i-1)-'0';
        }
    }
    total = odd + even;
    printf("total is %-5d\n",total);
    printf("odd is %-5d\n",odd);
    printf("even is %-5d\n",even);
    if(total%10==0)
    {
        printf("successful!!right card number!\n");
    }else
    {
        printf("this is not a card number!\n");
    }

}
void cardjudge(char *p)
{
    int lenth = len(p);
   // int lenth = 0;
    if(lenth%2 == 0)
    {
        even_card(p,lenth);
    }else
    {
        odd_card(p,lenth);
    }
}
int main()
{
   char p[] = "5432123456788881";
   char *a = "356827027232780";
   printf("%d\n",len(p));
    cardjudge(p);
    cardjudge(a);
    return 0;
}
